﻿using System;
using System.Windows.Forms;
using Logic;
using System.Threading;

namespace SampleDB
{
    /// <summary>
    /// SampleDBクラス
    /// </summary>
    public partial class HelthManager : Form
    {
        public HelthManager()
        {
            InitializeComponent();
        }

        #region イベント
        /// <summary>
        /// ロード時イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SampleDB_Load(object sender, EventArgs e)
        {
            // GridDataの設定
            Thread thread = new Thread(new ThreadStart(SetGridData));
            thread.Start();
        }

        /// <summary>
        /// 登録ボタン押下時処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 

        delegate void InitCtrlDelegate();
        private void updateloop()
        {
            var logic = new SampleDBLogic();
            int i;
            for (i = 0; i < 10; i++)
            {
                logic.update(textid.Text, txtContents.Text, textHight.Text, textWight.Text, textBP_low.Text, textBP_high.Text, textSleepTime.Text);
            }
                SetGridData();
                InitCtrl();
        }


        private void delete()
        {
            var logic = new SampleDBLogic();
            int i;
            for (i = 0; i < 10; i++)
            {
                logic.delete(textid.Text);
            }
                SetGridData();
                InitCtrl();                      
        }


        private void InitCtrl()
        {
            if (InvokeRequired)
            {
                Invoke(new InitCtrlDelegate(InitCtrl));
            }
            //テキストをクリアする
            txtContents.Text = String.Empty;
            textHight.Text = String.Empty;
            textBP_low.Text = String.Empty;
            textBP_high.Text = String.Empty;
            textSleepTime.Text = String.Empty;
            textWight.Text = String.Empty;
        }
        private void loop()
        {
            var logic = new SampleDBLogic();
            int i;           
            for (i = 0; i < 10; i++)
            {
                logic.Regist(textid.Text, txtContents.Text, textHight.Text, textWight.Text, textBP_low.Text, textBP_high.Text, textSleepTime.Text);
            }
            SetGridData();
            InitCtrl();
        }

        private void btnRegist_Click(object sender, EventArgs e)
        {
           //   var logic = new SampleDBLogic();
            // データ登録をする
            Thread thread = new Thread(new ThreadStart(loop));
            thread.Start();
            }

        #endregion

        #region private
        /// <summary>
        /// Gridにデータを設定する
        /// </summary>
        private void SetGridData()
        {
            var logic = new SampleDBLogic();
            // データを取得する
            var dataList = logic.GetData();

            BindingSource bs = new BindingSource();
            if (dataList != null && dataList.Count > 0)
            {
                foreach (var item in dataList)
                {
                    bs.Add(item);
                }
            }
            BindingGridData(bs);
           
           // gridData.DataSource = bs;
        }

        #endregion

        delegate void BindingGridDataDelegate(BindingSource bs);
        private void BindingGridData(BindingSource bs)
        {
            
            // UIスレッド以外から呼ばれた場合
            if (InvokeRequired)
            {
               
                Invoke(new BindingGridDataDelegate(BindingGridData), bs);
                return;
            }

            gridData.DataSource = bs;
           
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
           // var logic = new SampleDBLogic();
            Thread thread = new Thread(new ThreadStart(updateloop));
            thread.Start();
            
            // GridDataの再設定
            btnRegist.Enabled = false;
            SetGridData();
            btnRegist.Enabled = true;              
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
           // var logic = new SampleDBLogic();
            // データ登録をする
            Thread thread = new Thread(new ThreadStart(delete));
            thread.Start();
          //  logic.delete(textid.Text);

            // GridDataの再設定
            btnRegist.Enabled = false;
            SetGridData();
            btnRegist.Enabled = true;
           }

     
    }
}
